<?php
//面板工具箱
function baidupan_tool_control()
{
	$page = admin_url('admin.php?page=baidupan-tool');
	$error = new WP_Error;

	//备份数据库
	if (isset($_POST['action']) && 'backup' == $_POST['action']) {
		$backup = baidupan_backup_mysql();
		if ($backup->get_error_message('error')) {
			$error->add('error', $backup->get_error_message('error'));
		} else {
			$error->add('info', '备份数据库成功：' . $backup->get_error_message('info'));
		}
	}

	//初始同步
	if (isset($_POST['action']) && 'initialize' == $_POST['action']) {
		if (function_exists('set_time_limit')) {
			set_time_limit(0);
			if (function_exists('ignore_user_abort')) {
				ignore_user_abort(true);
			}
			$attachments = baidupan_get_attachment_ids();
			if ($attachments) {
				$msg = baidupan_initialize_sync($attachments);
				$error->add('error', $msg);
			} else {
				$error->add('error', '没有找到任何附件。');
			}
		} else {
			$error->add('error', '该功能需要set_time_limit函数支持，请联系主机服务商开启。');
		}
	}
?>

	<link rel="stylesheet" href="<?php echo plugins_url('baidupan/css/control.css'); ?>">

	<div class="mywrap wp-clearfix">
		<?php
		if ($error->has_errors()) {
			if ($error->get_error_message('error')) {
				echo '<div class="notice is-dismissible error">' . $error->get_error_message('error') . '</div>';
			} else {
				echo '<div class="notice is-dismissible success">' . $error->get_error_message('info') . '</div>';
			}
		}
		?>

		<form class="entry" method="post" action="<?php echo $page; ?>">
			<h3>备份数据库</h3>
			<ul>
				<li>在插件设置中可以设置定时备份；</li>
				<li>更新文章后，可以使用本功能手动备份数据库到<a href="https://pan.baidu.com">百度网盘</a>。</li>
			</ul>
			<p>
				<input class="button" type="submit" value="备份数据库" onclick="disabled=true; submit();">&nbsp;&nbsp;
				<button class="button" id="dump" onclick="disabled=true;">下载数据库</button>
				<input type="hidden" name="action" value="backup">
			</p>
		</form>

		<hr>

		<form class="entry" method="post" action="<?php echo $page; ?>">
			<h3>初始同步</h3>
			<ul>
				<li>初始同步用于初次安装插件时，将以往上传的附件同步到百度网盘；</li>
				<li>安装插件之后上传的附件会实时同步到百度网盘；</li>
				<li>
					<?php
					if (function_exists('ignore_user_abort')) {
						echo '开始执行任务后，关闭网页也不影响任务执行；';
					} else {
						echo '联系主机商开启ignore_user_abort函数，关闭网页也不影响任务执行；';
					}
					?>
				</li>
				<li>任务执行完毕会发送邮件到<?php echo get_bloginfo('admin_email'); ?>。</li>
			</ul>
			<p>
				<input class="button" type="submit" value="开始同步" onclick="disabled=true; submit();">
				<input type="hidden" name="action" value="initialize">
			</p>
		</form>

	</div>

	<script type="text/javascript">
		//下载数据库
		jQuery('#dump').click(function() {
			jQuery.ajax({
				cache: false,
				type: 'POST',
				url: ajaxurl,
				data: {
					'action': 'dump'
				},
				dataType: 'text',
				async: true,
				success: function(data) {
					data = atob(data);
					let arr = new Uint8Array(data.length);
					for (let i = 0; i < data.length; i++) {
						arr[i] = data.charCodeAt(i);
					}
					let blob = new Blob([arr], {
						type: 'application/zip'
					});

					let elink = document.createElement('a');
					elink.download = '<?php echo 'db_' . DB_NAME . '_' . wp_date('j') . '.zip'; ?>';
					elink.style.display = 'none';
					elink.href = window.URL.createObjectURL(blob);
					document.body.appendChild(elink);
					elink.click();
					window.URL.revokeObjectURL(elink.href);
					document.body.removeChild(elink);
				},
				error: function(xhr, thrownError) {
					alert("Status：" + xhr.status + "\nState：" + xhr.readyState + "\nThrew：" + thrownError);
				}
			});

		});
	</script>

<?php } ?>